<template>
  <div>
    <Form ref="form" :model="form" :rules="formRule" inline>
      <FormItem prop="account">
        <Input type="text" v-model="form.account" placeholder="账号">
          <Icon type="ios-person-outline" slot="prepend"></Icon>
        </Input>
      </FormItem>
      <FormItem prop="password">
        <Input type="password" v-model="form.password" placeholder="密码">
          <Icon type="ios-lock-outline" slot="prepend"></Icon>
        </Input>
      </FormItem>
      <FormItem>
        <Button type="primary" @click="handleLogin()">登录</Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
import { login } from '../../api/AdminApi'
export default {
  data () {
    return {
      form: {
        account: '',
        password: ''
      },
      formRule: {
        user: [
          { required: true, message: 'Please fill in the user name', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'Please fill in the password.', trigger: 'blur' },
          { type: 'string', min: 6, message: 'The password length cannot be less than 6 bits', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleLogin () {
      const resp = await login(this.form.account, this.form.password)
      localStorage.setItem('adminInfo', JSON.stringify(resp))
      this.$router.push({
        name: 'commodity_list'
      })
    }
  }
}
</script>

<style lang="scss" src="./Login.scss"></style>